﻿using Activity.Consts;
using Activity.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Activity.Helper.OAuthHelper;

namespace Activity.Helper
{
    public class WeiXinHelper
    {
        #region 获取AccessToken

        /// <summary>
        /// 获取Access_Token
        /// </summary>
        /// <returns></returns>
        public static string GetAccessToken()
        {
            string url = WeiXinConst.WeiXin_AccessTokenUrl;
            string result = HttpClientHelper.GetResponse(url);
            return result;
        }
        public static Oauth2AccessToken GetOauth2AccessToken(string code)
        {
            var url = string.Format(WeiXinConst.WeiXin_User_OpenIdUrl, code);
            var result = HttpClientHelper.GetResponse<Oauth2AccessToken>(url);
            return result;
        }
        #endregion

        #region 获取用户信息

        /// <summary>
        /// 根据用户Code获取用户信息（包括OpenId的简单信息）
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static WeiXinUserSampleInfo GetUserSampleInfo(string code)
        {
            string url = string.Format(WeiXinConst.WeiXin_User_OpenIdUrl, code);
            WeiXinUserSampleInfo info = HttpClientHelper.GetResponse<WeiXinUserSampleInfo>(url);
            return info;
        }

        /// <summary>
        /// 根据用户Code获取用户信息（包括OpenId的简单信息）
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static string GetUserOpenId(string code)
        {
            return GetUserSampleInfo(code).OpenId;
        }

        /// <summary>
        /// 根据OpenID 获取用户基本信息(需关注公众号）
        /// </summary>
        /// <param name="openId"></param>
		//[Obsolete("请使用：WeixinUserInfoApi.Get")]
  //      public static WeiXinUserInfo GetUserInfo(string openId)
  //      {
  //          //var token = AccessToken.Instance;
  //          string url = string.Format(WeiXinConst.WeiXin_User_GetInfoUrl, AccessToken.GetAccssTokenFromCacheServer(), openId);

  //          string result = HttpClientHelper.GetResponse(url);

  //          if (string.IsNullOrEmpty(result))
  //              return null;
  //          Log.WriteLog("获取用户（" + openId + "）信息：" + result);
  //          WeiXinUserInfo info = JsonConvert.DeserializeObject<WeiXinUserInfo>(result);
  //          //解析用户信息失败，判断 失败Code ，40001 为AccessToken失效，重新创建Token并获取用户信息
  //          if (info == null || AccessToken.CheckAndRefresh(info.ErrCode))
  //          {
  //              return GetUserInfoByNewAccessToken(openId);
  //          }

  //          return info;
  //      }

        /// <summary>
        /// 创建新的AccessToken 并获取用户信息
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        //private static WeiXinUserInfo GetUserInfoByNewAccessToken(string openId)
        //{
        //    string url = string.Format(WeiXinConst.WeiXin_User_GetInfoUrl, AccessToken.GetAccssTokenFromCacheServer(), openId);
        //    WeiXinUserInfo info = HttpClientHelper.GetResponse<WeiXinUserInfo>(url);
        //    return info;
        //}

        #endregion
    }
}
